<template>
  <div class="wrap">
    <!-- <img src="../assets/loginBg.png" class="bg" alt=""> -->
    <div class="center">
      <div>
        <img class="hy520" src="../assets/hy520.png" alt />
      </div>
      <div class="box">
        <div class="hy">
          <img src="../assets/code.jpg" class="loginLogo" alt srcset />
          <h3>登陆手机端管理订单</h3>
        </div>
        <div class="form">
          <div class="first">
            <p @click="login">登陆</p>
            <p class="active" @click="register">注册</p>
          </div>
          <div class="item">
            <img src="../assets/loginPhone.png" alt />
            <input type="text" v-model="phone" placeholder="输入手机号" />
          </div>
          <div class="item">
            <img src="../assets/pass.png" alt />
            <input type="text" v-model="checkCode" placeholder="输入验证码" />
            <span @click="getSendMS">{{ms}}</span>
          </div>
          <div class="item">
            <img src="../assets/pass.png" alt />
            <input type="text" v-model="password" @focus="changeType" placeholder="输入密码" />
          </div>
          <div class="item">
            <img src="../assets/pass.png" alt />
            <input type="text" v-model="password2" @focus="changeType" placeholder="确认密码" />
          </div>
          <div class="bok">
            <!-- <p @click="login">返回登陆</p> -->
            <p @click="register">注册</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
.hy520 {
  width: 250px;
}
.first {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  width: 100%;
  p {
    margin-left: 20px;
    color: #719bef;
    line-height: 30px;
    cursor: pointer;
  }
  .active {
    margin-top: 4px;
  }
  .active::after {
    content: "";
    display: block;
    height: 4px;
    background: #719bef;
    border-radius: 10px;
  }
}
.hy {
  margin-top: 74px;
  margin-right: 30px;
}
.hy520 {
  width: 250px;
}
.box {
  display: flex;
  align-items: flex-start;
}
.wrap {
  position: relative;
  min-height: 720px;
  margin: 0 auto;
  min-width: 940px;
  .bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    min-height: 720px;
  }
  .center {
    display: flex;
    justify-content:  space-around;
    background: #fff;
    align-items: center;
    width: 74%;
    margin: 0 auto;
    .loginLogo {
      width: 125px;
    }
    h3 {
      color: #719bef;
      font-size: 12px;
      font-weight: normal;
      width: 125px;
      text-align: center;
    }
    .form {
      margin-top: 80px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      box-shadow: 0px 1px 4px rgba(222, 219, 219, 0.3),
        0px 0px 20px rgba(0, 0, 0, 0.1);
      padding: 80px 100px;
      border-radius: 8px;
      .item {
        height: 60px;
        display: flex;
        align-items: center;
        border-bottom: 0.5px solid #719bef;
        width: 350px;
        margin-top: 20px;
        input {
          outline: none;
          background: transparent;
          border: none;
          display: block;
          margin-left: 15px;
          height: 100%;
          color: #719bef;
          font-size: 16px;
        }
        ::-webkit-input-placeholder {
          color: #719bef;
        }
        img {
          width: 20px;
        }
        span {
          display: block;
          padding: 4px 10px;
          color: #719bef;
          border-radius: 0.266667rem;
          border: 0.5px solid #719bef;
          white-space: nowrap;
          cursor: pointer;
        }
      }
      .bok {
        display: flex;
        align-items: center;
        margin-top: 50px;
        justify-content: space-between;
        width: 100%;
        p {
          width: 100px;
          height: 30px;
          border-radius: 30px;
          background: #fff;
          text-align: center;
          line-height: 30px;
          color: #719bef;
          cursor: pointer;
          border: 1px solid #719bef;
        }
        p:nth-of-type(2) {
          background: #fff;
          background: #719bef;
          color: #fff;
        }
      }
    }
  }
}
</style>
<script>
import { register, sendSms, login, chief } from "../utils/interface";
export default {
  data() {
    return {
      ms: "获取验证码",
      bok: true,
      phone: "",
      password: "",
      password2: "",
      checkCode: ""
    };
  },
  methods: {
    changeType(e) {
      e.srcElement.type = "password";
    },
    login() {
      this.$router.push("/");
    },
    register() {
      console.log(process.env.NODE_ENV);
      if (!/^1[3456789]\d{9}$/.test(this.phone)) {
        this.$alert("手机号码有误，请重填", "提示", {
          confirmButtonText: "确定"
        });
        return false;
      }
      if (!this.checkCode) {
        this.$alert("请输入验证码", "提示", {
          confirmButtonText: "确定"
        });
        return false;
      }
      if (!this.password) {
        this.$alert("请输入密码", "提示", {
          confirmButtonText: "确定"
        });
        return false;
      }
      if (this.password.length < 6) {
        this.$alert("密码不能少于6位", "提示", {
          confirmButtonText: "确定"
        });
        return false;
      }
      if (this.password !== this.password2) {
        this.$alert("两次密码输入不一致", "提示", {
          confirmButtonText: "确定"
        });
        return false;
      }
      register({
        account: this.phone,
        password: this.password,
        checkCode: this.checkCode
      }).then(res => {
        if (res.data.code == 200) {
          this.$alert("注册成功，去登陆", "提示", {
            confirmButtonText: "确定",
            callback: action => {
              this.$router.go(-1);
            }
          });
        }
      });

      // this.$router.push('/layout')
    },
    // 获取验证码
    getSendMS() {
      chief({ account: this.phone }).then(res => {
        if (res.data.data >= 1) {
          this.$confirm("该账号已存在请直接登陆", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.$router.go(-1);
            })
            .catch(() => {});
        }
        if (!/^1[3456789]\d{9}$/.test(this.phone)) {
          this.$alert("手机号码有误，请重填", "提示", {
            confirmButtonText: "确定"
          });
          return false;
        }
        if (!this.bok) {
          return false;
        }
        this.bok = false;
        let n = 60;
        sendSms({ account: this.phone }).then(res => {
          let timer = setInterval(() => {
            this.ms = `${n}秒后重新获取`;
            n--;
            if (n <= 0) {
              this.bok = true;
              this.ms = "重新获取验证码";
              n = 60;
              clearInterval(timer);
            }
          }, 1000);
        });
      });
    }
  }
};
</script>

